package com.ly.lw.styledo.utils;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

/**
 * 百度地图定位 工具类
 * 可以定位到 街道 和 经纬度
 * @修 改 人: v1.0
 * @日 期:
 * @修 订 版:
 */
public class LocationHelper {

    // 内置对象
    private static LocationHelper helper;

    private LocationClient locationClient;

    private BDLocationListener bdLocationListener;

    static Context context;

    //提供 定位接口
    private LocationLinstener locationLinstener;

    //    /
    public void setLocationLinstener(LocationLinstener locationLinstener) {
        this.locationLinstener = locationLinstener;
    }

    /**
     *
     */
    private LocationHelper(Context context) {
        /**
         * 注意:必须在主线程中初始化
         */
        this.context=context;
        //声明LocationClient类
        locationClient = new LocationClient(context);
        setLocationOption();
        bdLocationListener = new MyBDLocationListener();
        //注册监听函数
        locationClient.registerLocationListener(bdLocationListener);
        locationClient.start();
    }


    public static LocationHelper getInstance(Context context) {
        if (helper == null) {
            helper = new LocationHelper(context);
        }
        return helper;
    }

    public void init(Context context) {
        this.context = context;
    }

    public void start() {
        locationClient.start();
    }

    public void stop() {
        if (locationLinstener != null) {
            locationClient.unRegisterLocationListener(bdLocationListener);
        }
        if (locationClient != null) {
            locationClient.stop();
        }


    }

    private class MyBDLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
//            bdLocation.getAddrStr();
//            //获得当前定位的省份
//            bdLocation.getProvince();
//            //获取当前所在的城市
//            bdLocation.getCity();
//            bdLocation.getStreet();
//            //获取经度
//            bdLocation.getLongitude();
//            //获取纬度
//            bdLocation.getLatitude();

            if (locationLinstener != null) {
                locationLinstener.locationLinstener(bdLocation.getLongitude(), bdLocation.getLatitude());
                locationLinstener.locationLinstenerSendCity(bdLocation.getProvince(), bdLocation.getCity(), bdLocation.getStreet());
            }


        }
    }

    /**
     * 初始化
     */
    private void setLocationOption() {
        LocationClientOption option = new LocationClientOption();
        //坐标系
        option.setCoorType("BD09ll");
        //高精度
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        //设置返回坐标的时间间隔
        option.setScanSpan(5 * 1000);
        /**
         * 是否返回地址位置
         */
        option.setIsNeedAddress(true);
        /**
         *是否使用grs定位
         */
        option.setOpenGps(true);
        locationClient.setLocOption(option);
    }

    //提供 定位接口
    public interface LocationLinstener {
        public void locationLinstener(double longitude, double latitude);

        public void locationLinstenerSendCity(String province, String city, String street);

    }


}
